const app = getApp()
const api = require('../../config/api.js'); 
const util = require('../../utils/util.js'); 
const hub2 = require('../../utils/Hub2.js'); 
const log = require('../../utils/log.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
       YS: 0,
       SS: 0,
       MD: 0,
       wxPay: 0,
       socPay: 0,
       LocalMD: 0,
       RecordId: 0,
       OutTradeNo: '',
       StationId: 0,
       JYSJ: '',
       OrderId: '',
       TradeId: '',
       HYMDSJ: '',
       CDMDSJ: '',
       VipPay: false,
       PlateNo: '',
       INTime: '',
       StopTime: '',
       CSSJ: '',
       ScanOutYard: false, 
       de_btn: '--color: #08a3e9;',
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (option) { 
      let that = this;
      log.info(JSON.stringify(option))
      if(app.TenantName!=='与好智行')
      {
         this.setData({de_btn: '--color: #fe8d04;'});
      }
      this.setData({
        ScanOutYard: app.curthis.data.ScanOutYard
      })
      let OutTradeNo = app.OutTradeNo;
      let StationId = app.StationId;
      let RecordId = app.payRecordId;
      let From = '';
      if(OutTradeNo===''){
        try{
          OutTradeNo = option.OutTradeNo;
        }
        catch(ex)
        {}
     }
      if(StationId<=0){
         try{
              StationId = parseInt(option.StationId,10);
              if(StationId<=0){
                try{
                  StationId = wx.getStorageSync('StationId')
                }
                catch(ex)
                {}
              }
         }
         catch(ex)
         {}
      }
      if(RecordId<=0){
        try{
           RecordId = parseInt(option.RecordId,10);
           if(RecordId<=0){
            try{
               RecordId = wx.getStorageSync('RecordId')
            }
            catch(ex)
            {}
           }
        }
        catch(ex)
        {}
     }
     try{
      From = option.From;
     }
     catch(ex)
     {}
      log.info('正在加载支付成功结果......')
      util.showFullScreenLoading('正在加载......');
      util.request2(api.settlement, {  
        OutTradeNo : OutTradeNo,
        CC : 1,
        RecordID : RecordId,
        StationId : StationId,
        From: From 
    }, 'POST').then(function(res){
        if (res.status==1) {    
          util.tryHideFullScreenLoading(true);
          let Out_Trade_No = res.out_Trade_No.substring(res.out_Trade_No.length-15);
          let TradeId = res.tradeNo.substring(res.tradeNo.length-15);
          let JYSJ = res.gmtPayTime;
          let pays = JSON.parse(res.message);
          let wxpay = 0;
          let socpay = 0;
          pays.forEach(item=>{
             if(item.payType=="微信"){
                wxpay = item.Amount;
             }
             if(item.payType=="VIP"){
                socpay = item.Amount;
             }
          });
          // for(let i=0;i<pays.length;i++)
          // {
          //    if(pays[i].payType=="微信")
          //    {
          //       wxpay = pays[i].amount;
          //    }
          //    if(pays[i].payType=="会员积分")
          //    {
          //       socpay = pays[i].amount;
          //    }
          // }
          that.setData({
              YS: app.payYS,
              SS: app.paySS,
              MD: app.payMD,  
              wxPay: wxpay,
              socPay: socpay,
              JYSJ: JYSJ,
              OrderId: Out_Trade_No,
              TradeId: TradeId,
              HYMDSJ: app.HYMDSJ,
              LocalMD: app.LocalMD,
              CDMDSJ: app.CDMDSJ,
              VipPay: app.VipUseSoc=='Y',
              PlateNo: app.payPlateNo,
              INTime: app.payINTime,
              StopTime: app.payStopTime,
              CSSJ: app.OverTime,
          })
          hub2.startNotice();
        }
        else {   
          //发生错误
          log.error('加载结果错误-->'+JSON.stringify(res));
          util.tryHideFullScreenLoading(true);
          util.showErrorToast(res.message,999999);
        }
    },
    function(errmsg){  
      log.error('加载结果异常-->'+JSON.stringify(errmsg));
      util.tryHideFullScreenLoading(true);
      util.showErrorToast(errmsg,999999);
    }
    ); 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      wx.exitMiniProgram({
        success: (res) => {},
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})